<?php
namespace gnomephp;
/**
 * Logger class,
 * used for logging information to the logs directory.
 * @author peec
 *
 */
class Log{
	
	/**
	 * It will only log if in debug mode.
	 * @var int
	 */
	const MODE_DEBUG = 1;
	
	/**
	 * 
	 * It will also log in production mode.
	 * @var int
	 */
	const MODE_ALL = 2;
	
	/**
	 * 
	 * It will be a normal information message. Useful for just notifying things.
	 * @var int
	 */
	const INFO = 1;
	
	/**
	 * 
	 * Warning messages. If something went wrong, but it's not a fatal error.
	 * @var int
	 */
	const WARNING = 2;
	
	/**
	 * 
	 * Error message. Something went totally wrong. Fatal error!
	 * @var int
	 */
	const ERROR = 3;
	
	/**
	 * 
	 * Logs a message.
	 * @param string $msg A message to log.
	 * @param int $type Type of log message. Can be one of Log::INFO , Log::WARNING or Log::ERROR
	 * @param int $mode Decides whenever to log only in debug mode or all modes. Can be one of Log::MODE_ALL or Log::MODE_DEBUG
	 */
	static public function logMsg($msg, $type=Log::INFO, $mode=Log::MODE_ALL){
		if ($mode == Log::MODE_ALL || ($mode == Log::MODE_DEBUG && Configuration::get('application', 'debug_mode')==true)){
			self::writeMessage($msg, $type);
		}
	}
	
	/**
	 * 
	 * Writes a message to the log file.
	 * @param string $msg A message to log.
	 * @param int $type Type of log message. Can be one of Log::INFO , Log::WARNING or Log::ERROR
	 */
	static protected function writeMessage($msg, $type){
		
		$typeString = '';
		switch($type){
			case self::INFO:
				$typeString = 'INFO';
				break;
			case self::WARNING:
				$typeString = 'WARNING';
				break;
			default:
			case self::ERROR:
				$typeString = 'ERROR';
				break;
		}
		
		$message = "\r\n[ ".date('H:i:s', time())." ] [".$typeString."]   :    $msg";
		
		$fileName = date('d-m-y', time());
		file_put_contents(GNOME_APP_PATH . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR . $fileName .'.log', $message,FILE_APPEND);
	}
	
	
}